//引用网络请求
import {
    reqUsers,
    reqSwitch,
    reqAdd,
    reqAlter,
    reqSucc,
    reqDel,
    reqCheRole,
    reqDisRole
} from '../../api/index'
const state = {
    userList: {},
    alteruser: {}
};

const mutations = {
    GETUSERS(state, userList) {
        state.userList = userList
    },
    //修改按钮
    CHENCKSWITCH(state, id) {
        // console.log(id);
        state.userList.users.findIndex(item => {
            if (item.id == id) {
                item.mg_state = !item.mg_state;
            }
        })
    },
    //存储修改信息
    GETALTER(state, alteruser) {
        state.alteruser = alteruser
    }
};

const actions = {
    //获取用户列表
    async getUsers({
        commit
    }, params) {
        let res = await reqUsers(params)
        if (res.meta.status === 200) {
            commit('GETUSERS', res.data)
        }
    },
    //修改状态
    async getChenckSwitch(uId, type) {
        let res = await reqSwitch(type)
        // console.log(res);
        if (res.meta.status == 200) {
            return '已成功修改'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    //添加用户
    async getAdd({
        commit
    }, params) {
        let res = await reqAdd(params)
        // console.log(res);
        if (res.meta.status == 201) {
            return '创建成功'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    // 修改用户
    async getalter({
        commit
    }, id) {
        let res = await reqAlter(id)
        if (res.meta.status == 200) {
            commit('GETALTER', res.data)
        }
    },

    // 完成修改
    async getSucc({
        commit
    }, alteruser) {
        let res = await reqSucc(alteruser)
        if (res.meta.status == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    //删除
    async delUser({
        commit
    }, id) {
        let res = await reqDel(id)
        if (res.meta.status == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    // 完成分权
    async checkRole({
        commit
    }, {
        roleInd,
        idStr
    }) {
        // console.log({roleInd,idStr});
        let res = await reqCheRole(roleInd, idStr);
        if (res.meta.status == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    //完成分配角色
    async disRole({
        commit
    }, {
        id,
        rid
    }) {
        console.log(id, rid);
        let res = await reqDisRole(id, rid)
        if (res.meta.status == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'))
        }
    }
};

const getters = {

};

export default {
    state,
    mutations,
    actions,
    getters,
}